/**
 * @namespace
 */
aws.modules.walker = {
	listSubscribers: function() {
		return {
			"exec": this.exec,
			"keypress": this.keypress,
			"setRootWaypoint": this.setRootWaypoint,
			"goToNextWaypoint": this.goToNextWaypoint,
			"goToPreviousWaypoint": this.goToPreviousWaypoint,
			"updateWaypointValue": this.updateWaypointValue,
			"unsetRootWaypoint": this.unsetRootWaypoint,
			"reset": this.reset
		}
	},
	/**
	 * Checks if waypoint has code awailable to execute and executes it
	 */
	exec: function() {
		if (this.waypoint.onEnter && this.waypoint.onEnter.isAvailable() ) {
			this.waypoint.onEnter.exec();
		}
	},
	/**
	 * Sets first (root) waypoint
	 * @param {String} key Key of waypoint
	 */
	setRootWaypoint: function(key) {
		if (this.pathes[key]) {
			this.waypoint = this.pathes[key];
			this.waypoint.previous = this.waypoint[key];
			notify('goneToNextWaypoint');
			notify('rootWaypointSet');
		}
	},
	/**
	 * Moves to next waypoint
	 * @param {String} key Key of next waypoint. Can be omitted, if next waypoint is of "value" type
	 */
	goToNextWaypoint: function(key) {
		if (this.waypoint.next) {
			var prev = this.waypoint;
			if (typeof this.waypoint.next.value != 'undefined') {
				this.waypoint = this.waypoint.next;
			} else if (this.waypoint.next[key]) {
				this.waypoint = this.waypoint.next[key];
			} else {
				return;
			}
			this.waypoint.previous = prev;
			notify('goneToNextWaypoint');
		}
	},
	/**
	 * Moves to previous waypoint. Unsets root waypoint, if there are no previous
	 */
	goToPreviousWaypoint: function() {
		if (this.waypoint.previous) {
			this.waypoint = this.waypoint.previous;
			notify('goneToPreviousWaypoint');
		} else {
			notify('unsetRootWaypoint');
		}
	},
	/**
	 * Changes value of current waypoint (which should be of "value" type)
	 * @param {String} value New value of waypoint
	 */
	updateWaypointValue: function(value) {
		this.waypoint.value = value;
		notify('waypointValueChanged');
	},
	/**
	 * Unsets root waypoint, so there are no waypoints choosed
	 */
	unsetRootWaypoint: function() {
		delete this.waypoint;
		notify('rootWaypointUnset');
	},
	/**
	 * Similar to unsetRootWaypoint, but doesn't send any notifications
	 */
	reset: function() {
		this.waypoint.value = '';
		delete this.waypoint;
	},
	/**
	 * Decides if any manipulations should be done with waypont when keyboard keys got pressed
	 * @param {String} code Code of pressed keyboard key
	 * @param {Object} e Event
	 */
	keypress: function(code, e) {
		if (code == 13 && this.waypoint) {
			notify('exec');
		} else if (code == 8 && this.waypoint) {
			if (this.waypoint.value) {
				notify('updateWaypointValue', [this.waypoint.value.substr(0, this.waypoint.value.length - 1)]);
			} 
			if (!this.waypoint.value) {
				notify('goToPreviousWaypoint');
			}
		} else {
			var key = String.fromCharCode(code).toLowerCase();
			if (key) {
				if (this.waypoint) {
					if (!this.waypoint.value) {
						notify('goToNextWaypoint', [key]);
					}
					if (typeof this.waypoint.value != 'undefined') {
						notify('updateWaypointValue', [this.waypoint.value + key]);
					}
				} else {
					notify('setRootWaypoint', [key]);
				}
			}
		}
	}
};
